package com.xcgame.app;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.xcgame.pp.activity.PicturePickerActivity;
import com.xcgame.s.ShareActivity;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    Button b_showShare;
    View v_root;

    EditText et_content;

    private static final int REQUEST_CODE = 3211;
    private ImageView iv_addPicture;
    private String picturePath;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b_showShare = (Button) findViewById(R.id.b_showShare);
        b_showShare.setOnClickListener(this);
        et_content = (EditText) findViewById(R.id.et_content);
        et_content.setText("分享测试！！");

        iv_addPicture = (ImageView) findViewById(R.id.iv_addPicture);
        iv_addPicture.setOnClickListener(this);
        v_root = findViewById(R.id.v_root);

    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.iv_addPicture) {
            PicturePickerActivity.start(this, REQUEST_CODE, 12, 2, picturePath);
        }
        if (id == R.id.b_showShare) {
//            SharePopupWindow sharePopupWindow =  SharePopupWindow.getInstance(this);
//            sharePopupWindow.showShare(v_root,"分享","http://www.baidu.com",et_content.getText().toString(),picturePath);
            String imageUrl = "http://b.hiphotos.baidu.com/image/pic/item/810a19d8bc3eb135aa449355a21ea8d3fc1f4458.jpg";

            v_root.setDrawingCacheEnabled(true);
            v_root.buildDrawingCache();  //启用DrawingCache并创建位图
            Bitmap bitmap = Bitmap.createBitmap(v_root.getDrawingCache()); //创建一个DrawingCache的拷贝，因为DrawingCache得到的位图在禁用后会被回收
            v_root.setDrawingCacheEnabled(false);  //禁用DrawingCahce否则会影响性能
            String path = Environment.getExternalStorageDirectory().getPath() +  "/test.JPEG";
            File file = saveFormerImage(bitmap, path);
            ShareActivity.start(this, "分享", "https://www.baidu.com", "分享内容", file);

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == REQUEST_CODE && resultCode == PicturePickerActivity.TAKE_PICTURE && data != null) {
            picturePath = data.getStringExtra("picturePath");
            Glide.with(this).load(picturePath).into(iv_addPicture);
        }
        if (requestCode == REQUEST_CODE && resultCode == PicturePickerActivity.TAKE_ALBUM && data != null) {
            picturePath = data.getStringExtra("picturePath");
            Glide.with(this).load(picturePath).into(iv_addPicture);
        }
        if (requestCode == REQUEST_CODE && resultCode == PicturePickerActivity.DELETE_PICTURE) {
            picturePath = "";
            iv_addPicture.setImageBitmap(null);
        }
    }


    /**
     * 将Bitmap保存到指定路径
     *
     * @param bitmap
     * @param saveSrc
     * @return
     */
    public static File saveFormerImage(Bitmap bitmap, String saveSrc) {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);

        File saveFile = new File(saveSrc);
        if (!saveFile.exists()) {
            saveFile.mkdirs();
            saveFile.delete();
        }
        if (saveFile.exists()) {
            saveFile.delete();
        }

        try {
            FileOutputStream fos = new FileOutputStream(saveFile);
            byte[] bitmapBytes = baos.toByteArray();
            fos.write(bitmapBytes);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return null;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }

        return saveFile;
    }
}
